﻿layui.use(['form', 'layer', 'element'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        element = layui.element;

    function renderForm() {
        layui.use('form', function () {
            var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
            form.render();
        });
    }

    $(".coursefile").show();
    $(".courseexam").hide();

    form.on("radio(CourseTypeFilter)", function (data) {
        if (data.value == 0) {
            $(".coursefile").show();
            $(".courseexam").hide();
        } else if (data.value == 1) {
            $(".coursefile").hide();
            $(".courseexam").show();
        }
    });

    form.on("select(TaskId)", function (data) {
        var taskId = $(".TaskId").val();
        $('#CategoryId').empty();//先清空分类下拉框
        renderForm();
        console.log($(".CategoryId option"));
        if (taskId != "") {
            $.ajax({
                type: 'POST',
                url: '/Course/GetTask/',
                data: { taskId: taskId },
                dataType: 'json',
                success: function (categoryList) {
                    $.each(categoryList, function (i, category) {
                        console.log(category);
                        var option = "<option value=" + category.CategoryId + ">" + category.Name + "</option>";
                        console.log(category.CategoryId + category.Name);
                        $(".CategoryId").append(option);
                        renderForm();//表单重新渲染
                    });
                    $(".CategoryId").siblings("div.layui-form-select").find("dd:first").click();
                    $("#HandCategory").val($(".CategoryId option:selected").text());
                }
            })
        }
    });

    form.on("select(CategoryId)", function (data) {
        var select_text = data.elem[data.elem.selectedIndex].text;
        $("#HandCategory").val(select_text);
        $("#CategoryId").next().find("dl").css({ "display": "none" });
        form.render();
    });

    form.on("submit(addCourse)", function (data) {
        var actualCategoryId;
        if ($("#HandCategory").val() == $(".CategoryId option:selected ").text())
            actualCategoryId = $(".CategoryId").val();
        else
            actualCategoryId = "";
        var form = new FormData();
        form.append("TaskId", $(".TaskId").val());
        form.append("TaskName", $(".TaskId option:selected").text());
        form.append("CategoryName", $("#HandCategory").val());
        form.append("CategoryId", actualCategoryId);
        //form.append("CategoryDescription", $(".CategoryDescription").val());
        form.append("CourseName", $(".CourseName").val());
        form.append("CourseDescription", $(".CourseDescription").val());
        form.append("CoursePeriod", $(".CoursePeriod").val());
        form.append("CourseScore", $(".CourseScore").val());
        form.append("CourseType", $('input[name="CourseType"]:checked').val());
        form.append("FileId", $(".CourseFile").val());
        form.append("FileName", $(".CourseFile option:selected").text());
        form.append("ExamId", $(".ExamFile").val());
        $.ajax({
            type: 'POST',
            url: '/Course/Add/',
            data: form,
            dataType: "json",
            async: false,
            processData: false,
            contentType: false,
            headers: {
                "X-CSRF-TOKEN-Gerald": $("input[name='AntiforgeryKey_Gerald']").val()
            },
            success: function (res) {//res为相应体,function为回调函数
                var alertIndex;
                alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                    location.reload();
                    top.layer.close(alertIndex);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
        return false;
    });
});